<?php

namespace app\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use \common\models\Works;
use \common\models\WorksComment;
use \common\models\WorksCollection;

/**
 * Default controller for the `api` module
 */
class WorksController extends Controller
{
    public $enableCsrfValidation = false;
    /**
     * 获取首页数据 返回json字符串
     */
    public function actionDetail()
    {
        $worksid = Tools::GetSafeParam('worksid', 0, 0);
        if($worksid == 0){
            $output['success'] = 0;
            $output['msg'] = "作品ID有误！";
            Tools::outputjson($output);
        }
        $output['success'] = 1;
        $output['msg'] = "请求成功！";
        $output['data'] = \common\models\Works::getDetails($worksid);
        
        $userid = Yii::$app->user->isGuest ? 0:Yii::$app->user->identity->id;
        //设置点击量和阅读记录
        Works::setUserRead($worksid, $userid);
        
        Tools::outputjson($output);
    }
    
    public function actionPublish(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        $title = \common\models\Tools::GetSafeParam('title', '', 1);
        $content = \common\models\Tools::GetSafeParam('content', '', 1);
        $category = \common\models\Tools::GetSafeParam('category', 0, 0);
        
        
        if(empty($title)){
            $output['success'] = 0;
            $output['msg'] = "作品标题不能为空！";
            Tools::outputjson($output);
        }
        if(empty($content)){
            $output['success'] = 0;
            $output['msg'] = "内容不能为空！";
            Tools::outputjson($output);
        }
        
        //存在该标题的作品时，进入添加章节环节
        $exist = \common\models\Works::getUserWorksExist($title,Yii::$app->user->identity->id);
        if(!empty($exist)){
            $output['success'] = 0;
            $output['msg'] = "作品标题已发布过！";
            Tools::outputjson($output);
        }
        
        $works = new \common\models\Works();
        $works->title = $title;
        $works->cate = $category;
        $works->content = $content;
        $works->authorid = Yii::$app->user->identity->id;
        $works->author = Yii::$app->user->identity->username;
        $works->addtime = time();
        if($works->save()){
            $output['success'] = 1;
            $output['msg'] = "发布成功！等待审核......";
            Tools::outputjson($output);
        }else{
            var_dump($works->errors);exit;
            $output['success'] = 0;
            $output['msg'] = "发布失败，原因：".$works->errors;
            Tools::outputjson($output);
        }
    }
    
    public function actionComment(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        $worksid = \common\models\Tools::GetSafeParam('worksid', 0, 0);
        $content = \common\models\Tools::GetSafeParam('content', '', 1);
        $type = \common\models\Tools::GetSafeParam('type', 0, 0);
        $touserid = \common\models\Tools::GetSafeParam('touserid', 0, 0);
        $touser = \common\models\Tools::GetSafeParam('touser', '', 1);
        $author = \common\models\Tools::GetSafeParam('author', '', 1);
        $authorid = \common\models\Tools::GetSafeParam('authorid', 0, 0);
        
        if($content == ""){
            $output['success'] = 0;
            $output['msg'] = "评论内容不能为空！";
            Tools::outputjson($output);
        }
        
        $model = new WorksComment();
        $model->type = $type;
        $model->worksid = $worksid;
        $model->authorid = $authorid;
        
        $model->content = $content;
        if($type == 1){//留言为评论时
            $touserid = $authorid;
            $touser = $author;
        }
        $model->to_userid = $touserid;
        $model->to_user = $touser;
        $model->userid = Yii::$app->user->identity->id;
        $model->user = Yii::$app->user->identity->username;
        
        if($authorid == Yii::$app->user->identity->id){
            $model->isread = 1;
        }
        
        $model->addtime = time();
        if($model->save()){
            $output['success'] = 1;
            $output['msg'] = "发布成功！等待审核......";
            $output['userid'] = Yii::$app->user->identity->id;
            $output['user'] = Yii::$app->user->identity->username;
            $output['touserid'] = $touserid;
            $output['touser'] = $touser;
            $output['id'] = $model->id;
            
            Tools::outputjson($output);
        }else{
            Tools::outputjson($model->errors);
        }
        
    }
    
    public function actionCommentown(){
        $output = [];
        $output['success'] = 1;
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        $commentid = \common\models\Tools::GetSafeParam('commentid', 0, 0);
        $comments = WorksComment::findOne(['id'=>$commentid]);
        
        if(empty($comments)){
            $output['success'] = 0;
            $output['msg'] = "评论不存在！";
            Tools::outputjson($output);
        }
        $isOwner = 0;
        if($comments->userid == Yii::$app->user->identity->id){
            $isOwner = 1;
        }
        
        $output['data'] = ['isOwner'=>$isOwner];
        Tools::outputjson($output);
    }
    
    
    public function actionCommentdelete(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        $commentid = \common\models\Tools::GetSafeParam('commentid', 0, 0);
        $comments = WorksComment::findOne(['id'=>$commentid]);
        
        $output = [];
        $output['success'] = 1;
        if($comments->userid != Yii::$app->user->identity->id){
            $output['success'] = 0;
            $output['msg'] = "没有权限删除此评论！";
            Tools::outputjson($output);
        }else{
            $comments->delete();
            $output['msg'] = "删除成功！";
            Tools::outputjson($output);
        }
    }
    
    
    /***
     * 添加喜欢的作品
     */
    public function actionCollection(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        $userid = Yii::$app->user->identity->id;
        $worksid = \common\models\Tools::GetSafeParam('worksid', 0, 0);
        $exist = \common\models\WorksCollection::checkExist($userid, $worksid);
        if($exist){
            $exist = \common\models\WorksCollection::deleteExist($userid, $worksid);
            $output['success'] = 1;
            $output['msg'] = "已取消喜欢";
            $output['code'] = 0;
            Tools::outputjson($output);
        }
        else{
            $model = new WorksCollection();
            $model->userid = $userid;
            $model->worksid = $worksid;
            $model->addtime = time();
            if($model->save()){
                Works::collectionPlus($worksid);
                $output['success'] = 1;
                $output['msg'] = "已喜欢";
                $output['code'] = 1;
                Tools::outputjson($output);
            }else{
                $output['success'] = 1;
                $output['msg'] = "喜欢失败：".$model->errors;
                Tools::outputjson($output);
            }
        }
    }
    
    /***
     * 作品搜索
     */
    public function actionSearch(){
        $keywords = \common\models\Tools::GetSafeParam('keywords', "", 1);
        $data_title = Works::getWorksByTitle($keywords);
        $data_author = Works::getWorksByAuthor($keywords);
        $output['success'] = 1;
        $output['msg'] = "搜索成功！";
        $output['data'] = array_merge($data_title,$data_author);
        Tools::outputjson($output);
    }
}
